﻿using UnityEngine;
using UnityEditor;

namespace CGF
{
    public class ChangeToSprite
    {
        [MenuItem("Assets/转为 Sprite", false)]
        static void Execute()
        {
            Object[] objects = Selection.GetFiltered<Object>(SelectionMode.DeepAssets);
            for (int i = 0; i < objects.Length; i++)
            {
                if (objects[i] is Texture2D)
                {
                    ChangeTo(objects[i] as Texture2D);
                }
            }
            Debug.Log("转为 Sprite 完成！");
        }

        static void ChangeTo(Texture2D tex)
        {
            string path = AssetDatabase.GetAssetPath(tex);
            TextureImporter textureIm = AssetImporter.GetAtPath(path) as TextureImporter;
            textureIm.textureType = TextureImporterType.Sprite;
            textureIm.spriteImportMode = SpriteImportMode.Single;
            textureIm.alphaIsTransparency = true;
            AssetDatabase.ImportAsset(path);
        }
    }
}